﻿AB.namespace("SD_BarcodeScan.Mgr");
SD_BarcodeScan.Mgr = {
    commandHandler: function (command, record) {
        var FId = record.data.FId;
        switch (command) {
            case "Edit":
               
                break;
            case "Delete":
                
                break;
        }
    },
    successHandler: function (form, action) {
        //successHandler(form, action, this.successCallback);
    },
    failureHandler: function (form, action, failureCallback) {
        failureHandler(form, action);
    },
    successCallback: function () {
    },
    failureCallback: function () {
    },
    formStoreLoaded: function (store, records) {
        var formObj = Ext.getCmp("EditFormPanel");
        formStoreLoaded(store, records, formObj);
        BillTypeStore.load();
    },
    saveMsg: function () {
        if (FBillDate.getValue() == "") {
            Ext.Msg.show({
                title: '操作失败',
                msg: '请选择销售日期!',
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
            return;
        }

        if (FCustomerName.getValue() == "") {
            Ext.Msg.show({
                title: '操作失败',
                msg: '请输入客户名称!',
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
            return;
        }

        var FIdT = FIdH.getValue();
        var FNumberT = FNumber.getValue();
        var FBillDateT = Ext.util.Format.date(FBillDate.getValue(), 'Y-m-d');
        var FBusinessCategoryIdT = FBusinessCategoryId.getValue();
        var FCustomerNameT = FCustomerName.getValue();
        var FCustomerMobileT = FCustomerMobile.getValue();
        var FCustomerPhoneT = FCustomerPhone.getValue();
        var FCustomerAddressT = FCustomerAddress.getValue();
        var FRemarkT = FRemark.getValue();

        var FDetails = "";
        var view = SaleOrderDetailGrid.getView().ds;
        if (view != null) {
            var mark = view.data.items;
            for (var i = 0; i < mark.length; ++i) {

                if (i == mark.length - 1) {
                    FDetails += "{FId:'" + mark[i].data.FId + "',FBarcode:'" + mark[i].data.FBarcode + "'}";
                }
                else {
                    FDetails += "{FId:'" + mark[i].data.FId + "',FBarcode:'" + mark[i].data.FBarcode + "'},";
                }
            }
        }

        var msg = "{FId:'" + FIdT
                    + "',FNumber:'" + FNumberT
                    + "',FBillDate:'" + FBillDateT
                    + "',FBusinessCategoryId:'" + FBusinessCategoryIdT
                    + "',FCustomerName:'" + FCustomerNameT
                    + "',FCustomerMobile:'" + FCustomerMobileT
                    + "',FCustomerPhone:'" + FCustomerPhoneT
                    + "',FCustomerAddress:'" + FCustomerAddressT
                    + "',FRemark:'" + FRemarkT
                    + "',FDetails:[" + FDetails + "]}";

        Ext.Ajax.request({
            url: "/SaleOrder/SaveSaleOrderMsg",
            method: 'post',
            params: {
                msg: msg
            },
            success: function (result) {
                if (!isTimeOut(result)) {
                    var response = Ext.util.JSON.decode(result.responseText);
                    if (response.success == false) {
                        ajaxFailureHandler(response);
                    } else {
                        FIdH.setValue(response.result);
                        SaleOrderEidtStore.reload();
                        SaleOrderDetailListStore.reload();
                    }
                }
            }
        });
    },
    resetEditData: function () {
        FIdH.setValue("");
        EditFormPanel.form.reset();

        FBillDate.setValue(Ext.util.Format.date(new Date(), "Y/m/d"));

        var mark = BillTypeStore.data.items;
        FBusinessCategoryId.setValue(mark[0].data.FID);

        SaleOrderDetailListStore.removeAll();
    },
    approveOrder: function () {
        var FIdT = FIdH.getValue();
        if (FIdT == "") {
            return;
        }
        Ext.Msg.confirm('提示', '确定要审核该销售订单吗?', function (btn) {
            if (btn == "yes") {
                Ext.Ajax.request({
                    url: "/SaleOrder/ApproveOrder",
                    method: 'post',
                    params: {
                        FId: FIdT
                    },
                    success: function (result) {
                        if (!isTimeOut(result)) {
                            var response = Ext.util.JSON.decode(result.responseText);
                            if (response.success == false) {
                                ajaxFailureHandler(response);
                            } else {
                                Ext.Msg.alert('操作信息', "审核成功!");
                            }
                        }
                    }
                });
            }
        });
    },
    unApproveOrder: function () {
        var FIdT = FIdH.getValue();
        if (FIdT == "") {
            return;
        }
        Ext.Msg.confirm('提示', '确定要反审核该销售订单吗?', function (btn) {
            if (btn == "yes") {
                Ext.Ajax.request({
                    url: "/SaleOrder/UnApproveSaleOrder",
                    method: 'post',
                    params: {
                        FId: FIdT
                    },
                    success: function (result) {
                        if (!isTimeOut(result)) {
                            var response = Ext.util.JSON.decode(result.responseText);
                            if (response.success == false) {
                                ajaxFailureHandler(response);
                            } else {
                                Ext.Msg.alert('操作信息', "反审核成功!");
                            }
                        }
                    }
                });
            }
        });
    },
    barcodeScan: function (field, e) {
        if (e.getKey() == Ext.EventObject.ENTER) {
            var barcode = FBarcodeTFS.getValue();
            if (!checkBarcodeIsExist(barcode)) {
                SaleOrderDetailGrid.insertRecord(0, { FId: '', FParentId: '', FBarcode: barcode });
                showDetailMsg(OperationMsgTF, "");
            } else {
                showDetailMsg(OperationMsgTF, "条码已存在列表中!");
            }
            FBarcodeTFS.setValue("");
        }
    },
    deleteSelectedBarcode: function () {
        Ext.Msg.confirm('提示', '确定要删除选中的条码吗?', function (btn) {
            if (btn == "yes") {
                SaleOrderDetailGrid.deleteSelected();
            }
        });
    },
    barcodeScanInstall: function (field, e) {
        if (e.getKey() == Ext.EventObject.ENTER) {
            var barcode = FBarcodeTFS.getValue();
            if (!checkBarcodeIsExist(barcode)) {
                Ext.Ajax.request({
                    url: "/SaleOrder/GetSaleOrderByFBarcode",
                    method: 'post',
                    params: {
                        FBarcode: barcode
                    },
                    success: function (result) {
                        if (!isTimeOut(result)) {
                            var response = Ext.util.JSON.decode(result.responseText);
                            if (response.success == false) {
                                ajaxFailureHandler(response);
                            } else {
                                if (response.extraParamsResponse.Result == "true") {
                                    FNumberDF.setValue(response.extraParamsResponse.FNumber);
                                    FBillDateDF.setValue(response.extraParamsResponse.FBillDate);
                                    FCustomerNameDF.setValue(response.extraParamsResponse.FCustomerName);
                                    FCustomerPhoneDF.setValue(response.extraParamsResponse.FCustomerPhone);
                                    FCustomerMobileDF.setValue(response.extraParamsResponse.FCustomerMobile);
                                    FCustomerAddressDF.setValue(response.extraParamsResponse.FCustomerAddress);
                                   
                                    SaleOrderDetailGrid.insertRecord(0, { FId: '', FParentId: '', FBarcode: barcode });
                                    showDetailMsg(OperationMsgTF, "");
                                } else {
                                    FNumberDF.setValue("");
                                    FBillDateDF.setValue("");
                                    FCustomerNameDF.setValue("");
                                    FCustomerPhoneDF.setValue("");
                                    FCustomerMobileDF.setValue("");
                                    FCustomerAddressDF.setValue("");

                                    showDetailMsg(OperationMsgTF, "找不到条码对应的出货记录(批发)！");
                                }
                            }
                        }
                    },
                    failure: failureHandler
                });
            } else {
                showDetailMsg(OperationMsgTF, "条码已存在列表中!");
            }
            FBarcodeTFS.setValue("");
        }
    }
}

var showDetailMsg = function (item,msg) {
    item.setValue(msg);
}

var checkBarcodeIsExist = function (barcodeNew) {
    var view = SaleOrderDetailGrid.getView().ds;
    if (view != null) {

        var mark = view.data.items;
        for (var i = 0; i < mark.length; ++i) {
            var barcodeOld = mark[i].data.FBarcode;
            if (barcodeNew == barcodeOld) {
                return true;
            }
        }
    }
    return false;
}